package com.le.tester.tip.countdownlatch;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.le.tester.entity.CommentLike;
import com.le.tester.utils.BigDecimalUtil;
import io.netty.buffer.PoolSubpageMetric;
import io.swagger.models.auth.In;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;

/**
 * createTime：2022/3/26 14:01
 * description：1.让多个线程等待；2.让单个线程等待
 */
public class CountDownLatchDemo {
    //通过倒计时来控制线程的执行，直到0就可以执行
    public void racing1() {
        //裁判的使用，为了让所有选手都在用一个起跑线上，此时充当的角色是裁判罢了
        CountDownLatch countDownLatch = new CountDownLatch(1);

        //弄五次
        for (int i = 0; i < 5; i++) {
            //每个线程进行被同时挂起了
            Thread thread = new Thread(() -> {
                try {
                    //线程被挂起，等待count计数器变成0继续，所有的线程进行了等待
                    countDownLatch.await();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e.getMessage());
                }

                System.out.println(Thread.currentThread().getName() + " is exec" + " current time is " + LocalDateTime.now());
            });
            //基本上是同一个时刻开始执行
            thread.start();
        }
        //准备倒计时，五个线程会同时的exec
        countDownLatch.countDown();
    }

    //单个线程进行等待
    public void racing2() throws InterruptedException {
        //countDownLatch充当选手的时候
        //模拟的是五个选手
        CountDownLatch countDownLatch = new CountDownLatch(5);
        for (int i = 0; i < 5; i++) {
            Thread thread = new Thread(() -> {
//                Thread.sleep(ThreadLocalRandom.current().nextInt(500), TimeUnit.MILLISECONDS);
                try {
                    TimeUnit.MILLISECONDS.sleep(ThreadLocalRandom.current().nextInt(5000));
                    System.out.println(Thread.currentThread().getName() + " is exec" + " current time is " + LocalDateTime.now());

                    countDownLatch.countDown();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e.getMessage());
                }
            });
            thread.start();
        }
        countDownLatch.await();
        System.out.println("等待成绩公布。。。");
    }


    public static void main(String[] args) throws InterruptedException {
//        String jsonStr = "{\"default\":[{\"specs\":{\"specId\":19,\"specName\":\"pstype\",\"isMultiple\":false,\"specWord\":\"产品类型\",\"isNecessary\":true},\"specsvalues\":[\"类型1\"]},{\"specs\":{\"specId\":\"\",\"specName\":\"\",\"isMultiple\":false,\"specWord\":\"\",\"isNecessary\":true},\"specsvalues\":[]}]}";
//
//        JSONObject jsonObject = JSONObject.parseObject(jsonStr);
//        JSONArray aDefault = JSONObject.parseArray(jsonObject.get("default").toString());
//        for (int i = 0; i < aDefault.size(); i++) {
//            Map map = JSONObject.parseObject(aDefault.get(i).toString(), Map.class);
//            Object specs = JSONObject.parse(map.get("specs").toString());
//            Map map1 = JSONObject.parseObject(specs.toString(), Map.class);
//            String specId = String.valueOf(map1.get("specId"));
//            if (StringUtils.isBlank(specId)) {
//                System.out.println("遇到了空值");
//                return;
//            }
//
//        }

        //StringBuffer stringBuffer = new StringBuffer("01100000000");
        //StringBuffer resultBuffer = new StringBuffer();
//        String tempStr = "1,1,0,0,0";
//        List<Integer> tempList = new ArrayList<>();
//        //StringBuffer stringBuffer = new StringBuffer();
//        int space = 2;
//        int resultIndex = 0;
//        String temp = "10:00,10:30,11:00,11:30,12:00";
//        String oldTimeSchedule = "10:00";
//        String[] timeSplit = temp.split(",");
//        int length = timeSplit.length;
//
//        //找出开始的下标
//        for (int i = 0; i < length; i++) {
//            if (oldTimeSchedule.equals(timeSplit[i])) {
//                resultIndex = i;
//            }
//        }
//
//        System.out.println("开始的下标：" + resultIndex);
//        System.out.println("结束的下标：" + (resultIndex + space));
//        //String[] split = stringBuffer.toString().split(",");
//
//        String[] splitStr = tempStr.split(",");
//        for (String s : splitStr) {
//            tempList.add(Integer.valueOf(s));
//        }
//
//        for (int i = resultIndex; i < resultIndex + space; i++) {
//            tempList.set(i, 0);
//        }
//
//        System.out.println(tempList.toString());
//
//        String time = "2022-04-21 09:00:00";
//
//        String timeTemp = "9:00-21:00";
//        LocalDateTime businessStartTimeParse = LocalDateTime.parse(time, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
//        System.out.println("success");
//
//        String[] split = timeTemp.split("-");
//        String start = split[0];
//        String end = split[1];
//
//        System.out.println("start" + start.length());
//        System.out.println("end" + end.length());
////        String fontString = stringBuffer.substring(0, resultIndex);
////        String endString = stringBuffer.substring(resultIndex + space);
////        String resultStr = fontString + stringBuffer + endString;
////        System.out.println(resultStr);
////        String fontString = stringBuffer.substring(0, resultIndex);
////        String endString = stringBuffer.substring(resultIndex + space);
////        System.out.println(resultBuffer);
//        String result = StringUtils.join(tempList, ",");
//        System.out.println(result);
//
//        List<Integer> integerList = new ArrayList<>();
//        integerList.add(2);
//        integerList.add(3);
//
//        Integer integer = integerList.stream().reduce(Integer::sum).orElse(0);
//        System.out.println("integer is " + integer);

        Map<String, String> map = new HashMap<>();
        map.put("biz_content", "{\"merId\":\"3089991539900DG\",\"orderId\":\"01a3e9ea3b044ef7a125fc1681999e34\",\"cmbOrderId\":\"100422041116165810031433\",\"userId\":\"N088879903\",\"txnAmt\":\"1\",\"currencyCode\":\"156\",\"payType\":\"ZF\",\"openId\":\"2088902130242582\",\"txnTime\":\"20220411161658\",\"endDate\":\"20220411\",\"endTime\":\"161736\",\"dscAmt\":\"0\",\"thirdOrderId\":\"2022041122001442581415834713\",\"payBank\":\"ALIPAYACCOUNT\",\"buyerLogonId\":\"159****1459\"}");

        String bizContent = map.get("biz_content");
        JSONObject jsonObject = JSONObject.parseObject(bizContent);
        System.out.println(jsonObject);
        Object orderId = jsonObject.get("orderId");
        System.out.println(orderId);

        String price = "0.01";
        String s = String.valueOf(String.format("%.0f", new BigDecimal(price).multiply(new BigDecimal("100"))));
        System.out.println("price is " + s);
    }
}
